package com.gopay.bis.manager.bank.ebank;

import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.bis.common.util.BisPayCheckUtils;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.bobj.ebank.BobjB2cMessage;
import com.gopay.common.domain.bank.bobj.ebank.BobjMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.BankManagerRemoteService;
import com.gopay.remote.bank.businesses.BankService;

public class BobjB2cManager implements BankManagerRemoteService{
	
	@Resource(name = "bobjB2cBankServiceClient")
    private BankService bobjB2cBankServiceClient;
	
	@Resource(name = "bobjB2cBankServiceClientHna")
    private BankService bobjB2cBankServiceClientHna;
	
    @Resource(name = "bobjConfig")
    private Map<String, String> bobjConfig; 
    
	@Override
	public Object sign(BaseMessage msg) throws GopayException {
	    BobjMessage bobjMessage = (BobjMessage)msg;
	    bobjMessage = buildBobjMsg(msg, msg.getSpecial());
	    BankLogUtils.printB2CManagerSignSendLog(bobjMessage);
	    String sign;
	    if(bobjMessage.isHna()){
	    	sign = (String) this.bobjB2cBankServiceClientHna.sign(bobjMessage);
	    }else{
	    	sign = (String) this.bobjB2cBankServiceClient.sign(bobjMessage);
	    }
	    bobjMessage.setMerReqData(sign);
	    BankLogUtils.printB2CManagerSignReturnLog(bobjMessage);
	    return bobjMessage;
	}

	@Override
	public Object verify(BaseMessage msg) throws GopayException {
		BankLogUtils.printB2CManagerVerifySendLog(msg);
		BobjB2cMessage bobjMessage;
		if(msg.isHna()){
			bobjMessage = (BobjB2cMessage) this.bobjB2cBankServiceClientHna.verify(msg);
	    }else{
	    	bobjMessage = (BobjB2cMessage) this.bobjB2cBankServiceClient.verify(msg);
	    }
		BankLogUtils.printB2CManagerVerifyReturnLog(msg, bobjMessage);
        return bobjMessage;
	}

	@Override
	public Object query(Object obj) throws GopayException {
		BobjB2cMessage msg = (BobjB2cMessage)obj;
		if(BisPayCheckUtils.checkExternalOrderID(msg.getOrderId())){
			msg.setOrderNum(msg.getOutOrderId());
		}
		BobjB2cMessage bobjMessage = buildBobjMsg(msg, msg.getSpecial());
		BankLogUtils.printB2CManagerQuerySendLog(bobjMessage, "");
		if(msg.isHna()){
			bobjMessage = (BobjB2cMessage) this.bobjB2cBankServiceClientHna.query(bobjMessage);
	    }else{
	    	bobjMessage = (BobjB2cMessage) this.bobjB2cBankServiceClient.query(bobjMessage);
	    }
	    BankLogUtils.printB2CManagerQueryReturnLog(bobjMessage);
	    return bobjMessage;
	}
	
	/**
     * 构建返回对象 默认是为了支付
     * 
     * @param message
     * @param sign
     * @return
     */
    private BobjB2cMessage buildBobjMsg(BaseMessage message, String bizType) {
    	BobjB2cMessage bobjMessage = (BobjB2cMessage) message;
            if (StringUtils.equals(bizType, BankConstants.BIZ_QUERY_TYPE)) {// 查询类
            	if(BisPayCheckUtils.checkExternalOrderID(message.getOrderId())){
            	 bobjMessage.setResponseUrl((String) this.bobjConfig.get("BOBJ.B2C.QUERY.BIS.NOTIFYURL"));
            	 bobjMessage.setNotifyUrl((String) this.bobjConfig.get("BOBJ.B2C.QUERY.BIS.NOTIFYURL"));
            	}else{
            	 bobjMessage.setResponseUrl((String) this.bobjConfig.get("BOBJ.B2C.QUERY.NOTIFYURL"));
            	 bobjMessage.setNotifyUrl((String) this.bobjConfig.get("BOBJ.B2C.QUERY.NOTIFYURL"));
            	}
            	 bobjMessage.setActionType((String) this.bobjConfig.get("BOBJ.QUERY.ACTIONTYPE"));
            	 bobjMessage.setBankUrl(bobjMessage.getQueryUrl());
            } else {
            	bobjMessage.setNotifyUrl(bobjMessage.getPayRuturnUrl());
            	bobjMessage.setResponseUrl(bobjMessage.getPayRuturnUrl());
            	bobjMessage.setActionType((String) this.bobjConfig.get("BOBJ.PAY.ACTIONTYPE"));
            	bobjMessage.setBankUrl(bobjMessage.getPayUrl());
            }
            bobjMessage.setGoodsType((String) this.bobjConfig.get("BOBJ.GOODSTYPE"));
            bobjMessage.setNetType((String) this.bobjConfig.get("BOBJ.B2C.NETTYPE"));
        return bobjMessage;
    }

	@Override
	public Object dayend(BaseMessage base) throws GopayException, Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object refund(BaseMessage base) throws GopayException {
		// TODO Auto-generated method stub
		return null;
	}


}
